subroutine ccsdt_sym_aab_dg_l2(ie, t1aa, t1bb, t2aa, t2ab, t2ba, t2aa2, t2ab2, t2ba2, & 
                    iijkaaa, iijkaab, iijkaba, iabijaa, iabijab, iabijba, &
                    tmp1, tmp2, tmp3, tmp4, tmp5, tmpe, &
                    iabciaa, iabciab, iabciba, & 
                    icore, e4t, e5st, ig)
use mod_iop
use mod_orbit
use mod_size
use mod_symm
use mod_t
implicit none
integer prec2, isd2, igpu2, totgpu2
common/precsoc/prec2, isd2, igpu2, totgpu2 
real*8, intent(in) :: e4t, e5st
real*8, intent(in) :: ie(*), t1aa(*), t1bb(*), t2aa(*), t2ab(*), t2ba(*)
real*8, intent(in) :: t2aa2(*), t2ab2(*), t2ba2(*) 
real*8, intent(in) :: iijkaaa(*), iijkaab(*), iijkaba(*)
real*8, intent(in) :: iabijaa(*), iabijab(*), iabijba(*)
real*8, intent(in) :: iabciaa(*), iabciab(*), iabciba(*)
real*8, intent(in) :: tmp1(*), tmp2(*), tmp3(*), tmp4(*), tmp5(*), tmpe(*)
!
real*8, intent(in) :: icore(*)
real*8, intent(in), device :: ig(*)
integer i0, i1, i2, i3, nsize, iter, ileft
integer i4, i5, i6, i7, i8, i9, i10
integer i11, i12, i13, i14, i15, i16, i17, i18, i19, i20
integer i21, i22, i23, i24, i25, i26, i27, i28, i29, i30 
integer i31, i32, i33, i34, icopy, it, itend
integer nabcaa, nabcab, nabcba, nabiaa, nabiab, nabiba, nabcbaj
integer irrepi, numi, nabcaai, nabcbai, iread, ioffaai, ioffbai
integer ioffvvvoaa(noa,8), ioffvvvoab(noa, 8), ioffvvvoba(noa, 8)
integer ioffvvoo2aa(noa, 8), ioffvvoo2ab(noa, 8), ioffvvoo2ba(noa, 8) 
integer ioffvoaa(noa, 8), ioffvobb(noa, 8)
integer ioffvoooaa(noa,noa,8,8), ioffvoooab(noa, noa, 8, 8)
integer ioffvoooba(noa,noa, 8, 8), ioffvvooaa(noa,noa,8,8)
integer ioffvvooab(noa, noa, 8, 8), ioffvvooba(noa, noa, 8, 8)
!
integer irrepki, irrepij, irrepjk, irrepabc, numj, numk
integer irrepj, irrepk, i, j, k, iofftmp
integer, external :: idsymsoc, irpdso
real*8, external :: nrm2so
   igpu=0; igpu2=0
i0 = 1
i1 = i0 + nvvoo
i2 = i1 + nvvoo
!
!calcioff
!for iabci--------------
call idxpqr_s(ioffvvvoaa, noa, vrta, vrta, 0, vrta, popa, 1, 1)
call idxpqr_s(ioffvvvoab, noa, vrta, vrtb, 1, vrta, popb, 1, 1)
call idxpqr_s(ioffvvvoba, noa, vrtb, vrta, 1, vrtb, popa, 1, 1)
!for t2aa2--------------
call idxpqr_s(ioffvvoo2aa, noa, vrta, vrta, 0, popa, popa, 1, 1)
call idxpqr_s(ioffvvoo2ab, noa, vrta, vrtb, 1, popa, popb, 1, 1)
call idxpqr_s(ioffvvoo2ba, noa, vrtb, vrta, 1, popb, popa, 1, 1)
!for t1
call idxpqr_s(ioffvoaa, noa, zorb, zorb, 1, vrta, popa, 1, 1)
call idxpqr_s(ioffvobb, noa, zorb, zorb, 1, vrtb, popb, 1, 1)
!for t2aa and iabij
call idxpq_rs(ioffvvooaa, noa, noa, vrta, vrta, 1, popa, popa, 1, 1)
call idxpq_rs(ioffvvooab, noa, noa, vrta, vrtb, 1, popa, popb, 1, 1)
call idxpq_rs(ioffvvooba, noa, noa, vrtb, vrta, 1, popb, popa, 1, 1)
!for iijka
call idxpq_rs(ioffvoooaa, noa, noa, popa, vrta, 1, popa, popa, 1, 1)
call idxpq_rs(ioffvoooab, noa, noa, popa, vrtb, 1, popa, popb, 1, 1)
call idxpq_rs(ioffvoooba, noa, noa, popb, vrta, 1, popb, popa, 1, 1)
!write(6,*)'ioffvooo', ioffvooo(2,3, 1,1)
!
!=======get t1
!
call getallso(t1aa, 1, 'T1AA____')
call getallso(t1bb, 1, 'T1BB____')
!
!=======get t2
!
call getallso(icore(i0), 1, 'T2AA____')
call iexpso(icore(i0), icore(i1), vrta, vrta, 0, 0, popa, popa, 0, 1, 1)
call sstgenso(icore(i1), t2aa2, nsize, nvvaa, zorb,  popa, popa, & 
             icore, 1, '1324')
call iexpso(icore(i1), t2aa, vrta, vrta, 0, 1, popa, popa, 1, 0, 1)
!
call getallso(t2ab, 1, 'T2AB____')
call sstgenso(t2ab, t2ab2, nsize, nvvab, zorb, popa, popb, & 
              icore, 1, '1324')
call isymtrso(t2ab, icore, vrta, vrtb, 1, 1, popa, popb, 1, 0, 1) 
call isymtrso(icore, t2ba, vrtb, vrta, 1, 0, popa, popb, 1, 1, 1) 
call sstgenso(t2ba, t2ba2, nsize, nvvab, zorb, popb, popa, & 
              icore, 1, '1324')
if(ldebug) then 
nsize = idsymsoc(1, vrta, vrta, 1, popa, popa, 1)
write(6,*)'norm of t2aa', nrm2so(nsize, t2aa, 1)
nsize = idsymsoc(1, vrta, vrta, 0, popa, popa, 1)
write(6,*)'norm of t2aa2', nrm2so(nsize, t2aa2, 1)
nsize = idsymsoc(1, vrta, vrtb, 1, popa, popb, 1)
write(6,*)'norm of t2ab', nrm2so(nsize, t2ab, 1)
write(6,*)'norm of t2ab2', nrm2so(nsize, t2ab2, 1)
write(6,*)'norm of t2ba', nrm2so(nsize, t2ba, 1)
write(6,*)'norm of t2ba2', nrm2so(nsize, t2ba2, 1)
endif
!
!=======get iabij(a<b,i<j) and transfer to iabij(a,b,i,j) 
!
call getallso(icore(i0), 1, 'ABIJAA__')
call iexpso(icore(i0), icore(i1), vrta, vrta, 0, 0, popa, popa, 0, 1, 1)
call iexpso(icore(i1), iabijaa,   vrta, vrta, 0, 1, popa, popa, 1, 0, 1)
call getallso(iabijab, 1, 'ABIJAB__')
call isymtrso(iabijab, icore, vrta, vrtb, 1, 1, popa, popb, 1, 0, 1) 
call isymtrso(icore, iabijba, vrtb, vrta, 1, 0, popa, popb, 1, 1, 1) 
!
!=======get iijka(i<j,k,a) and transfer to iijka(k,a,i,j)
!
call getallso(icore(i0), 1, 'IJKAAA__')
call itranspso(icore(i0), icore(i1), popa, popa, 0, popa, vrta, 1, 1)
call iexpso(icore(i1), iijkaaa, popa, vrta, 1, 0, popa, popa, 0, 1, 1)
!
call getallso(icore(i0), 1, 'IJKAAB__')
call itranspso(icore(i0), iijkaab, popa, popb, 1, popa, vrtb, 1, 1)
call getallso(icore(i0), 1, 'IJKABA__')
call itranspso(icore(i0), iijkaba, popb, popa, 1, popb, vrta, 1, 1)
!
if(ldebug) then 
nsize = idsymsoc(1, popa, popa, 1, vrta, popa, 1) 
write(6,*)'norm of iijkaaa', nrm2so(nsize, iijkaaa, 1)
endif
! 
!=======get iabci aa, ab and ba
!
!call getallso(iabci, 1, 'ABCIAA__')
!nsize = idsymsoc(1, vrta, vrta, 0, vrta, popa, 1) 
!write(6,*)'norm of iabciaa', nrm2so(nsize, iabci, 1)
do irrepi = 1, nirrep
   numi = popa(irrepi)
   do i = 1, numi
   iofftmp = ioffvvvoaa(i, irrepi)
   call getpqrso(iabciaa(iofftmp), i, irrepi, 1, 'ABCIAA__')
   iofftmp = ioffvvvoba(i, irrepi)
   call getpqrso(iabciba(iofftmp), i, irrepi, 1, 'ABCIBA__')
   enddo
enddo
do irrepi = 1, nirrep
   numi = popb(irrepi)
   do i = 1, numi
   iofftmp = ioffvvvoab(i, irrepi)
   call getpqrso(iabciab(iofftmp), i, irrepi, 1, 'ABCIAB__')
   enddo
enddo
if(ldebug) then 
nsize = idsymsoc(1, vrta, vrta, 0, vrta, popa, 1) 
write(6,*)'norm of iabciaa', nrm2so(nsize, iabciaa, 1)
nsize = idsymsoc(1, vrta, vrtb, 1, vrta, popb, 1) 
write(6,*)'norm of iabciab', nrm2so(nsize, iabciab, 1)
nsize = idsymsoc(1, vrtb, vrta, 1, vrtb, popa, 1) 
write(6,*)'norm of iabciba', nrm2so(nsize, iabciba, 1)
endif
!
do irrepk = 1, nirrep 
do irrepj = 1, nirrep
do irrepi = 1, nirrep 
   irrepij = dirprd(irrepi, irrepj) 
   irrepjk = dirprd(irrepj, irrepk) 
   irrepki = dirprd(irrepk, irrepi) 
   irrepabc = dirprd(irrepij, irrepk)
   nabcaa = max(idsymsoc(irrepi, vrta, vrta, 0, vrta, zorb, 1), &
                idsymsoc(irrepj, vrta, vrta, 0, vrta, zorb, 1))
   nabcba = max(idsymsoc(irrepi, vrta, vrtb, 1, vrtb, zorb, 1), &
                idsymsoc(irrepj, vrta, vrtb, 1, vrtb, zorb, 1))
   nabcaai = idsymsoc(irrepi, vrta, vrta, 0, vrta, zorb, 1)
   nabcbai = idsymsoc(irrepi, vrtb, vrta, 1, vrtb, zorb, 1)
   nabcbaj = idsymsoc(irrepj, vrtb, vrta, 1, vrtb, zorb, 1)
   nabcab = idsymsoc(irrepk, vrta, vrtb, 1, vrta, zorb, 1)
!
   nabiaa = max(idsymsoc(irrepi, vrta, vrta, 0, popa, zorb, 1), &
                idsymsoc(irrepj, vrta, vrta, 0, popa, zorb, 1))
   nabiba = max(idsymsoc(irrepi, vrta, vrtb, 1, popb, zorb, 1), &
                idsymsoc(irrepj, vrta, vrtb, 1, popb, zorb, 1))
   nabiab = idsymsoc(irrepk, vrta, vrtb, 1, popa, zorb, 1)
   numi = popa(irrepi)
   numj = popa(irrepj) 
   numk = popb(irrepk)
   i0  = 1
   i1  = i0  + nva+noa          ! iea
   i2  = i1  + nvb+nob          ! ieb
   i3  = i2  + nvomax           ! t1aa
   i4  = i3  + nvomax           ! t1bb
!
   i5  = i4  + nvvmax           ! t2aa_ij
   i6  = i5  + nvvmax           ! t2ab_ik
   i7  = i6  + nvvmax           ! t2ab_jk
   i8  = i7  + nvvmax           ! t2ba_ki
   i9  = i8  + nvvmax           ! t2ba_kj 
!
   i10 = i9  + nabiaa           ! t2aa2_i
   i11 = i10 + nabiaa           ! t2aa2_j
   i12 = i11 + nabiab           ! t2ab2_k
   i13 = i12 + nabiba           ! t2ba2_i
   i14 = i13 + nabiba           ! t2ba2_j
!
   i15 = i14 + nvomax           ! iijkaaa_ij
   i16 = i15 + nvomax           ! iijkaab_ik
   i17 = i16 + nvomax           ! iijkaab_jk
   i18 = i17 + nvomax           ! iijkaba_ki
   i19 = i18 + nvomax           ! iijkaba_kj
!
   i20 = i19 + nvvmax           ! iabijaa_ij
   i21 = i20 + nvvmax           ! iabijab_jk
   i22 = i21 + nvvmax           ! iabijab_ik
!
   i23 = i22 + nvvv             ! tmp1
   i24 = i23 + nvvv             ! tmp2
   i25 = i24 + nvvv             ! tmp3
   i26 = i25 + nvvv             ! tmp4
   i27 = i26 + nvvv             ! tmp5 
   i28 = i27 + nvvv             ! tmpe
!
   i29 = i28 + nvvv           ! iabciba_j
   i30 = i29 + nvvv           ! iabciaa_j 
   i31 = i30 + nvvv           ! iabciab_k
!
   igpuleft = igpusize - i31
   if(igpuleft<nvvv*2) then 
      write(6,*)'error!!!! GPU memory not enough'
      write(6,*)'needed memory', (i31+nvvv*2)/1024/1024/4/isd,'mb'
      stop
   endif
   iread = min(igpuleft/nvvv/2, nvvvo/nvvv+1)
   i32 = i31 + nvvv*iread           ! iabciba_i
   i33 = i32 + nvvv*iread           ! iabciaa_i 
!   write(6,*)'iread',iread  
!   write(6,*)'test gpu in bba_dg_l'
   call myicopy_g(ie, ig(i0), nva+noa)
   call myicopy_g(ie(nva+noa+1), ig(i1), nvb+nob) 
   call myicopy_g(t1aa, ig(i2), nvomax)
   call myicopy_g(t1bb, ig(i3), nvomax) 
!   write(6,*)'test gpu in bba_dg_l'
   igpu=1; igpu2=1
   if(irrepi>irrepj) cycle
   if(irrepi==irrepj) then
     iter = 0
     ileft = numi
     do it = 1, numi
        if(it==iter*iread+1) then 
        itend = it+iread-1
        iter = iter + 1
        icopy = min(iread, ileft)
        if(icopy<0) icopy = icopy+iread
        if(ldebug) then 
        write(6,'(A,I4, I4, I4)')'iter, iread and icopy',iter, iread, icopy
        endif
        call myicopy_g(iabciaa(ioffvvvoaa(it,irrepi)),ig(i32), nabcaai*icopy)
        call myicopy_g(iabciba(ioffvvvoba(it,irrepi)),ig(i31), nabcbai*icopy)
        ileft = numi-iter*iread
        ioffaai = 0
        ioffbai = 0

      do k = 1, numk 
          call myicopy_g(t2ab2(ioffvvoo2ab(k,irrepk)), ig(i11), nabiab)
          call myicopy_g(iabciab(ioffvvvoab(k,irrepk)),ig(i30), nabcab)  
      do j = 2, numj
          call myicopy_g(t2ab(ioffvvooab(j,k,irrepj,irrepk)), ig(i6), nvvmax)
          call myicopy_g(t2ba(ioffvvooba(k,j,irrepk,irrepj)), ig(i8), nvvmax)
          call myicopy_g(t2aa2(ioffvvoo2aa(j,irrepj)), ig(i10), nabiaa)
          call myicopy_g(t2ba2(ioffvvoo2ba(j,irrepj)), ig(i13), nabiba)
          call myicopy_g(iijkaab(ioffvoooab(j,k,irrepj,irrepk)),ig(i16), nvomax)
          call myicopy_g(iijkaba(ioffvoooba(k,j,irrepk,irrepj)),ig(i18), nvomax)  
          call myicopy_g(iabijab(ioffvvooab(j,k,irrepj,irrepk)),ig(i20), nvvmax)    
          call myicopy_g(iabciaa(ioffvvvoaa(j,irrepj)),ig(i29), nabcaa)    
          call myicopy_g(iabciba(ioffvvvoba(j,irrepj)),ig(i28), nabcba)
         ioffaai = 0
         ioffbai = 0 
      do i = 1, j-1 
!          write(6,*)'ig(28)', i, j, nrm2so(nabcbaj, ig(i28), 1)          
!          call myicopy_g(iabciba(ioffvvvoba(j,irrepj)),ig(i28), nabcba)
          if(i<it.or.i>itend) cycle
          call myicopy_g(t2aa(ioffvvooaa(i,j,irrepi,irrepj)), ig(i4), nvvmax)
          call myicopy_g(t2ab(ioffvvooab(i,k,irrepi,irrepk)), ig(i5), nvvmax)
          call myicopy_g(t2ba(ioffvvooba(k,i,irrepk,irrepi)), ig(i7), nvvmax)
!
          call myicopy_g(t2aa2(ioffvvoo2aa(i,irrepi)), ig(i9),  nabiaa)
          call myicopy_g(t2ba2(ioffvvoo2ba(i,irrepi)), ig(i12), nabiba)
!
          call myicopy_g(iijkaaa(ioffvoooaa(i,j,irrepi,irrepj)),ig(i14), nvomax)
          call myicopy_g(iijkaab(ioffvoooab(i,k,irrepi,irrepk)),ig(i15), nvomax)  
          call myicopy_g(iijkaba(ioffvoooba(k,i,irrepk,irrepi)),ig(i17), nvomax)  
!
          call myicopy_g(iabijaa(ioffvvooaa(i,j,irrepi,irrepj)),ig(i19), nvvmax)
          call myicopy_g(iabijab(ioffvvooab(i,k,irrepi,irrepk)),ig(i21), nvvmax)  
! 
      call ccsdt_calcaab_2(ig(i0), ig(i1), ig(i2-1+ioffvoaa(i,irrepi)), & 
          ig(i2-1+ioffvoaa(j,irrepj)), ig(i3-1+ioffvobb(k,irrepk)), &
!
          ig(i4), ig(i5), ig(i6), ig(i7), ig(i8), &
!
          ig(i9), ig(i10), ig(i11), ig(i12), ig(i13), &
!
          ig(i14), ig(i15), ig(i16), ig(i17), ig(i18), &
!
          ig(i19), ig(i20), ig(i21), &
!
          ig(i22), ig(i23), ig(i24), ig(i25), ig(i26), ig(i27),  &
!
          ig(ioffaai+i32), ig(i29), ig(i30), ig(ioffbai+i31), ig(i28), &
!
           irrepi, irrepj, irrepk, i, j, k,          &
          ioffvvvoaa, ioffvvvoab, ioffvvvoba,    &
          ioffvvoo2aa, ioffvvoo2ab, ioffvvoo2ba,&
          ioffvoaa, ioffvobb,&
          ioffvoooaa, ioffvoooab,&
          ioffvoooba, ioffvvooaa,&
          ioffvvooab, ioffvvooba,&
          ig(i33), e4t, e5st,vrta, vrtb, popa, popb, 3)
!          write(6,*)'ioffi',i, ioffaai, ioffbai
          ioffaai = ioffaai + nabcaai
          ioffbai = ioffbai + nabcbai
      enddo
      enddo
      enddo
      endif
      enddo
   elseif(irrepi<irrepj) then
     iter = 0
     ileft = numi
     do it = 1, numi
        if(it==iter*iread+1) then
        itend = it+iread-1
        iter = iter + 1
        icopy = min(iread, ileft)
        if(icopy<0) icopy = icopy+iread
        if(ldebug) then 
        write(6,'(A,I4, I4, I4)')'iter, iread and icopy',iter, iread, icopy
        endif
        call myicopy_g(iabciaa(ioffvvvoaa(it,irrepi)),ig(i32), nabcaai*icopy)
        call myicopy_g(iabciba(ioffvvvoba(it,irrepi)),ig(i31), nabcbai*icopy)
        ileft = numi-iter*iread
        ioffaai = 0
        ioffbai = 0

        do k = 1, numk
          call myicopy_g(t2ab2(ioffvvoo2ab(k,irrepk)), ig(i11), nabiab)
          call myicopy_g(iabciab(ioffvvvoab(k,irrepk)),ig(i30), nabcab)  
        do j = 1, numj 
          call myicopy_g(t2ab(ioffvvooab(j,k,irrepj,irrepk)), ig(i6), nvvmax)
          call myicopy_g(t2ba(ioffvvooba(k,j,irrepk,irrepj)), ig(i8), nvvmax)
          call myicopy_g(t2aa2(ioffvvoo2aa(j,irrepj)), ig(i10), nabiaa)
          call myicopy_g(t2ba2(ioffvvoo2ba(j,irrepj)), ig(i13), nabiba)
          call myicopy_g(iijkaab(ioffvoooab(j,k,irrepj,irrepk)),ig(i16), nvomax)
          call myicopy_g(iijkaba(ioffvoooba(k,j,irrepk,irrepj)),ig(i18), nvomax)
          call myicopy_g(iabijab(ioffvvooab(j,k,irrepj,irrepk)),ig(i20), nvvmax)
          call myicopy_g(iabciaa(ioffvvvoaa(j,irrepj)),ig(i29), nabcaa)
          call myicopy_g(iabciba(ioffvvvoba(j,irrepj)),ig(i28), nabcba)
            ioffaai = 0
            ioffbai = 0 
        do i = 1, numi
          if(i<it.or.i>itend) cycle
          call myicopy_g(t2aa(ioffvvooaa(i,j,irrepi,irrepj)), ig(i4), nvvmax)
          call myicopy_g(t2ab(ioffvvooab(i,k,irrepi,irrepk)), ig(i5), nvvmax)
          call myicopy_g(t2ba(ioffvvooba(k,i,irrepk,irrepi)), ig(i7), nvvmax)
!
          call myicopy_g(t2aa2(ioffvvoo2aa(i,irrepi)), ig(i9),  nabiaa)
          call myicopy_g(t2ba2(ioffvvoo2ba(i,irrepi)), ig(i12), nabiba)
!
          call myicopy_g(iijkaaa(ioffvoooaa(i,j,irrepi,irrepj)),ig(i14), nvomax)
          call myicopy_g(iijkaab(ioffvoooab(i,k,irrepi,irrepk)),ig(i15), nvomax)
          call myicopy_g(iijkaba(ioffvoooba(k,i,irrepk,irrepi)),ig(i17), nvomax)
!
          call myicopy_g(iabijaa(ioffvvooaa(i,j,irrepi,irrepj)),ig(i19), nvvmax)
          call myicopy_g(iabijab(ioffvvooab(i,k,irrepi,irrepk)),ig(i21), nvvmax)
! 
      call ccsdt_calcaab_2(ig(i0), ig(i1), ig(i2-1+ioffvoaa(i,irrepi)), &
          ig(i2-1+ioffvoaa(j,irrepj)), ig(i3-1+ioffvobb(k,irrepk)), &
!
          ig(i4), ig(i5), ig(i6), ig(i7), ig(i8), &
!
          ig(i9), ig(i10), ig(i11), ig(i12), ig(i13), &
!
          ig(i14), ig(i15), ig(i16), ig(i17), ig(i18), &
!
          ig(i19), ig(i20), ig(i21), &
!
          ig(i22), ig(i23), ig(i24), ig(i25), ig(i26), ig(i27),  &
!
          ig(ioffaai+i32), ig(i29), ig(i30), ig(ioffbai+i31), ig(i28), &
!
           irrepi, irrepj, irrepk, i, j, k,          &
          ioffvvvoaa, ioffvvvoab, ioffvvvoba,    &
          ioffvvoo2aa, ioffvvoo2ab, ioffvvoo2ba,&
          ioffvoaa, ioffvobb,&
          ioffvoooaa, ioffvoooab,&
          ioffvoooba, ioffvvooaa,&
          ioffvvooab, ioffvvooba,&
           icore, e4t, e5st,vrta, vrtb, popa, popb, 3)
          ioffaai = ioffaai + nabcaai
          ioffbai = ioffbai + nabcbai
        enddo
        enddo
        enddo
       endif
     enddo
     endif
enddo 
enddo
enddo
write(6,*)'e4t and e5st for aab', e4t, e5st 
   igpu=0; igpu2=0
return
end
